<?php
//from_2 DATE_FORMAT(from_2,'%d-%m-%Y')
   class manuim	
   {	
          
   
       public  $manuimFields = array(	
           "manuy_row" => "",
           "manuy_number" => "",
           "create_2" => "",
           "state" => "",
           "agent_person_number" => "",
           "person_number" => "",
           "from_2" => "",
           "until" => "",
           "manufacturer" => "",
           "model" => "",
           "license_plate" => "",
           "production_year" => "",
           "product_desc" => "",
           "product_price" => "",
           "paymen_type" => "",
           "agent_commission" => "",
           "from_agent_commission" => "",
           "total_payment" => "",
           "shulam" => "",
           "last" => "",
           "shulamToAgent" => "",
           "shulamToMetriaAgent" => "",
           "agentAmlaDate" => "", 
           "notification_approved" => "", 
           "sidx" => "manuy_number",
           "sord" => "asc",
           "rows" => "20",
           "page" => "1"
       );
     
        	public $manuimFieldNames = array("manuy_row","manuy_number","create_2","state","agent_person_number","person_number","manufacturer","model","license_plate","production_year","product_desc","product_price","from_2","until","paymen_type","agent_commission","from_agent_commission","total_payment","shulam","last","shulamToAgent","shulamToMetriaAgent","agentAmlaDate");			
    	public $manuimFieldNamesView = array("manuy_row","manuy_number","create_2","state","agent_person_number","person_number","manufacturer","model","license_plate","production_year","product_desc","product_price","paymen_type","agent_commission","from_agent_commission","total_payment","shulam","last","shulamToAgent","shulamToMetriaAgent","agentAmlaDate","notification_approved");								
     	public $manuimFieldNamesEdit = array("manuy_row","manuy_number","create_2","state","agent_person_number","person_number","from_2","until","manufacturer","model","license_plate","production_year","product_desc","product_price","paymen_type","agent_commission","from_agent_commission","total_payment","shulam","last","shulamToAgent","shulamToMetriaAgent");
           public $viewManuimPerAgentNames = array("manuy_row","manuy_number","create_2","state","manufacturer","model","license_plate","production_year","product_desc","product_price","paymen_type","agent_commission","from_agent_commission","total_payment","shulam","last", "shulamToAgent", "shulamToMetriaAgent","agentAmlaDate");
    	public $viewManuimof15DaysPastArray = array("manuy_row","manuy_number","manufacturer","model","license_plate","production_year","product_desc","product_price","paymen_type","agent_commission","from_agent_commission","total_payment", "shulamToAgent", "shulamToMetriaAgent","agentAmlaDate");
           public $viewManuimPerAgentNamesAmlot = array("manuy_row","manuy_number","create_2","state","manufacturer","model","license_plate","production_year","product_desc","product_price","paymen_type","agent_commission","from_agent_commission","total_payment","last", "shulamToAgent", "shulamToMetriaAgent");
   
       function __construct() 
       { 
           $this->manuimFields['from_2']= date('Y-m-d',strtotime(date("Y-m-d", time()) . " - 365 day"));
           $this->manuimFields['until']= date('Y-m-d',strtotime(date("Y-m-d", time()) . " + 365 day"));
           
           foreach($this->manuimFields as $fieldName => $value) 
               $this->manuimFields[(string)$fieldName]=( isset($_POST[(string)$fieldName]) ? $_POST[(string)$fieldName] : $this->manuimFields[(string)$fieldName]);	
       
       } 
       
           
   	public function  viewManuimPerAgentPerPerson()
   	{
   		if ( isset($_POST['person_number']) && isset($_SESSION['person_number']) )
   		{
                         // $today=date("Y-m-d");
                          $oneYearOn = date('Y-m-d',strtotime(date("Y-m-d", time()) . " + 365 day"));
                          $oneYearBack = date('Y-m-d',strtotime(date("Y-m-d", time()) . " - 365 day"));
   			#----- view and filter sql query generator  ------#
   			$filter_query="SELECT *, from_2 as from_2, until as until FROM manuy2client WHERE ";
   			$sidx = ( isset($_POST['sidx']) ? 'ORDER BY '.$_POST['sidx'] : "");	
   			$sord = ( isset($_POST['sord']) ? $_POST['sord'] : "");	
                           $from_2 = ( isset($_POST['from_2']) ? $_POST['from_2'] : $oneYearBack);	
   			$until = ( isset($_POST['until']) ? $_POST['until'] : $oneYearOn);
   			foreach ($this->viewManuimPerAgentNames as $fieldName) 
   			{
   				${(string)$fieldName} = ( isset($_POST[(string)$fieldName]) ? $_POST[(string)$fieldName] : "");	
   				if ($fieldName === end($this->viewManuimPerAgentNames))
                                   $filter_query .= "{$fieldName} LIKE '%".${(string)$fieldName}."%' AND agent_person_number={$_SESSION['person_number']} AND person_number={$_POST['person_number']} and from_2 BETWEEN '{$from_2}' and  '{$until}' {$sidx} {$sord} ;";
   				else	
   				$filter_query .= "{$fieldName} LIKE '%".${(string)$fieldName}."%' AND ";
   			}
   			//echo $filter_query;
   			#----- view and filter sql query generator  ------#
   			
   			#----- get data from DB ------#
   			require("../includes/openConnection.php"); 
   			$q = mysql_query($filter_query) or die(mysql_error());
   			require("../includes/closeConnection.php");
   			#----- get data from DB ------#
   			
   			#----- return json ------#
   			$rows = array();
   			while($r = mysql_fetch_assoc($q)) 
   				{	
   				//	$r['perosnid'] = $r['id'];
   				//  unset($r['id']);
   				$rows[] = $r;
   				}
   			print json_encode($rows);
   			#----- return json ------#
   		}
   
   		
   	}
        
   	public function  viewManuimPerAgentPerPersonSum()
   	{
   		if ( isset($_POST['person_number']) && isset($_SESSION['person_number']) )
   		{
                         // $today=date("Y-m-d");
                          $oneYearOn = date('Y-m-d',strtotime(date("Y-m-d", time()) . " + 365 day"));
                          $oneYearBack = date('Y-m-d',strtotime(date("Y-m-d", time()) . " - 365 day"));
   			#----- view and filter sql query generator  ------#
   			$filter_query="SELECT SUM(agent_commission) AS sum_agent_commission,SUM(total_payment) AS sum_total_payment,SUM(product_price) AS sum_product_price FROM manuy2client WHERE ";
   			$sidx = ( isset($_POST['sidx']) ? 'ORDER BY '.$_POST['sidx'] : "");	
   			$sord = ( isset($_POST['sord']) ? $_POST['sord'] : "");	
                        $from_2 = ( isset($_POST['from_2']) ? $_POST['from_2'] : $oneYearBack);	
   			$until = ( isset($_POST['until']) ? $_POST['until'] : $oneYearOn);
   			foreach ($this->viewManuimPerAgentNames as $fieldName) 
   			{
   				${(string)$fieldName} = ( isset($_POST[(string)$fieldName]) ? $_POST[(string)$fieldName] : "");	
   				if ($fieldName === end($this->viewManuimPerAgentNames))
                                   $filter_query .= "{$fieldName} LIKE '%".${(string)$fieldName}."%'  AND state<>"."'קפוא'"." AND agent_person_number={$_SESSION['person_number']} AND agent_person_number={$_SESSION['person_number']} AND person_number={$_POST['person_number']} and from_2 BETWEEN '{$from_2}' and  '{$until}' {$sidx} {$sord} ;";
   				else	
   				$filter_query .= "{$fieldName} LIKE '%".${(string)$fieldName}."%' AND ";
   			}
   			//echo $filter_query;
   			#----- view and filter sql query generator  ------#
   			
   			#----- get data from DB ------#
   			require("../includes/openConnection.php"); 
   			$q = mysql_query($filter_query) or die(mysql_error());
   			require("../includes/closeConnection.php");
   			#----- get data from DB ------#
   			
   			#----- return json ------#
   			$rows = array();
   			while($r = mysql_fetch_assoc($q)) 
   				{	
   				//	$r['perosnid'] = $r['id'];
   				//  unset($r['id']);
   				$rows[] = $r;
   				}
   			print json_encode($rows);
   			#----- return json ------#
   		}
   
   		
   	}
           
           	public function  viewManuimPerAgent()
   	{
                       
   		if (  isset($_SESSION['person_number']) )
   		{
   			#----- view and filter sql query generator  ------#
   			$oneYearOn = date('Y-m-d',strtotime(date("Y-m-d", time()) . " + 365 day"));
                          $oneYearBack = date('Y-m-d',strtotime(date("Y-m-d", time()) . " - 365 day"));
   			#----- view and filter sql query generator  ------#
   			$filter_query="SELECT *, from_2 as from_2, until as until FROM manuy2client WHERE ";
   			$sidx = ( isset($_POST['sidx']) ? 'ORDER BY '.$_POST['sidx'] : "");	
   			$sord = ( isset($_POST['sord']) ? $_POST['sord'] : "");	
                           $from_2 = ( isset($_POST['from_2']) ? $_POST['from_2'] : $oneYearBack);	
   			$until = ( isset($_POST['until']) ? $_POST['until'] : $oneYearOn);
   			foreach ($this->viewManuimPerAgentNames as $fieldName) 
   			{
   				${(string)$fieldName} = ( isset($_POST[(string)$fieldName]) ? $_POST[(string)$fieldName] : "");	
   				if ($fieldName === end($this->viewManuimPerAgentNames))
   				$filter_query .= "{$fieldName} LIKE '%".${(string)$fieldName}."%' AND agent_person_number={$_SESSION['person_number']} and from_2 BETWEEN '{$from_2}' and  '{$until}'  {$sidx} {$sord} ;";
   				else	
   				$filter_query .= "{$fieldName} LIKE '%".${(string)$fieldName}."%' AND ";
   			}
   			//echo $filter_query;
   			#----- view and filter sql query generator  ------#
   			
   			#----- get data from DB ------#
   			require("../includes/openConnection.php"); 
   			$q = mysql_query($filter_query) or die(mysql_error());
   			require("../includes/closeConnection.php");
   			#----- get data from DB ------#
   			
   			#----- return json ------#
   			$rows = array();
   			while($r = mysql_fetch_assoc($q)) 
   				{	
   				//	$r['perosnid'] = $r['id'];
   				//  unset($r['id']);
   				$rows[] = $r;
   				}
   			print json_encode($rows);
   			#----- return json ------#
   		}
   
   		
   	}
           			
           
           
           
           public function  viewManuimof15DaysPast()
   	{
                             
   		if (  isset($_SESSION['person_number']) )
   		{
   			#----- view and filter sql query generator  ------#
   			$days15fromNow = date('Y-m-d',strtotime(date("Y-m-d", time()) . " - 15 day"));
                          $today = date('Y-m-d',strtotime(date("Y-m-d", time()) ));
   			#----- view and filter sql query generator  ------#
   			$filter_query="SELECT *, from_2 as from_2, until as until FROM manuy2client WHERE ";
   			$sidx = ( isset($_POST['sidx']) ? 'ORDER BY '.$_POST['sidx'] : "");	
   			$sord = ( isset($_POST['sord']) ? $_POST['sord'] : "");	
                          // $from_2 = ( isset($_POST['from_2']) ? $_POST['from_2'] : $oneYearBack);	
   			//$until = ( isset($_POST['until']) ? $_POST['until'] : $oneYearOn);
   			foreach ($this->viewManuimof15DaysPastArray as $fieldName) 
   			{
   				${(string)$fieldName} = ( isset($_POST[(string)$fieldName]) ? $_POST[(string)$fieldName] : "");	
   				if ($fieldName === end($this->viewManuimof15DaysPastArray))
   				$filter_query .= "{$fieldName} LIKE '%".${(string)$fieldName}."%' AND state='פעיל' AND last=1 AND shulam=0 AND agent_person_number={$_SESSION['person_number']} and DATE_ADD(create_2, INTERVAL {$_POST['minRange']} DAY)  <= '{$today}' and DATE_ADD(create_2, INTERVAL {$_POST['maxRange']} DAY)  > '{$today}'   {$sidx} {$sord} ;";
   				else	
   				$filter_query .= "{$fieldName} LIKE '%".${(string)$fieldName}."%' AND ";
   			}
                          // echo $today;
                         //  echo $filter_query;
   			#----- view and filter sql query generator  ------#
   			
   			#----- get data from DB ------#
   			require("../includes/openConnection.php"); 
   			$q = mysql_query($filter_query) or die(mysql_error());
   			require("../includes/closeConnection.php");
   			#----- get data from DB ------#
   			
   			#----- return json ------#
   			$rows = array();
   			while($r = mysql_fetch_assoc($q)) 
   				{	
   				//	$r['perosnid'] = $r['id'];
   				//  unset($r['id']);
   				$rows[] = $r;
   				}
   			print json_encode($rows);
   			#----- return json ------#
   		}
   
   		
   	}
        
           
           public function  viewManuimof45DaysPast()
   	{
                             
   		if (  isset($_SESSION['person_number']) )
   		{
   			#----- view and filter sql query generator  ------#
   			$days15fromNow = date('Y-m-d',strtotime(date("Y-m-d", time()) . " - 15 day"));
                          $today = date('Y-m-d',strtotime(date("Y-m-d", time()) ));
   			#----- view and filter sql query generator  ------#
   			$filter_query="SELECT *, from_2 as from_2, until as until FROM manuy2client WHERE ";
   			$sidx = ( isset($_POST['sidx']) ? 'ORDER BY '.$_POST['sidx'] : "");	
   			$sord = ( isset($_POST['sord']) ? $_POST['sord'] : "");	
                          // $from_2 = ( isset($_POST['from_2']) ? $_POST['from_2'] : $oneYearBack);	
   			//$until = ( isset($_POST['until']) ? $_POST['until'] : $oneYearOn);
   			foreach ($this->viewManuimof15DaysPastArray as $fieldName) 
   			{
   				${(string)$fieldName} = ( isset($_POST[(string)$fieldName]) ? $_POST[(string)$fieldName] : "");	
   				if ($fieldName === end($this->viewManuimof15DaysPastArray))
   				$filter_query .= "{$fieldName} LIKE '%".${(string)$fieldName}."%' AND shulam=0 AND agent_person_number={$_SESSION['person_number']} and DATE_ADD(create_2, INTERVAL 45 DAY)  <= '{$today}' and DATE_ADD(create_2, INTERVAL 60 DAY)  > '{$today}'   {$sidx} {$sord} ;";
   				else	
   				$filter_query .= "{$fieldName} LIKE '%".${(string)$fieldName}."%' AND ";
   			}
                          // echo $today;
                         //  echo $filter_query;
   			#----- view and filter sql query generator  ------#
   			
   			#----- get data from DB ------#
   			require("../includes/openConnection.php"); 
   			$q = mysql_query($filter_query) or die(mysql_error());
   			require("../includes/closeConnection.php");
   			#----- get data from DB ------#
   			
   			#----- return json ------#
   			$rows = array();
   			while($r = mysql_fetch_assoc($q)) 
   				{	
   				//	$r['perosnid'] = $r['id'];
   				//  unset($r['id']);
   				$rows[] = $r;
   				}
   			print json_encode($rows);
   			#----- return json ------#
   		}
   
   		
   	}
        
           
           
           
           public function  viewManuimof60DaysPast()
   	{
                             
   		if (  isset($_SESSION['person_number']) )
   		{
   			#----- view and filter sql query generator  ------#
   			$days60fromNow = date('Y-m-d',strtotime(date("Y-m-d", time()) . " + 60 day"));
                          $today = date('Y-m-d',strtotime(date("Y-m-d", time()) ));
   			#----- view and filter sql query generator  ------#
   			$filter_query="SELECT *, from_2 as from_2, until as until FROM manuy2client WHERE ";
   			$sidx = ( isset($_POST['sidx']) ? 'ORDER BY '.$_POST['sidx'] : "");	
   			$sord = ( isset($_POST['sord']) ? $_POST['sord'] : "");	
                          // $from_2 = ( isset($_POST['from_2']) ? $_POST['from_2'] : $oneYearBack);	
   			//$until = ( isset($_POST['until']) ? $_POST['until'] : $oneYearOn);
   			foreach ($this->viewManuimof15DaysPastArray as $fieldName) 
   			{
   				${(string)$fieldName} = ( isset($_POST[(string)$fieldName]) ? $_POST[(string)$fieldName] : "");	
   				if ($fieldName === end($this->viewManuimof15DaysPastArray))
   				$filter_query .= "{$fieldName} LIKE '%".${(string)$fieldName}."%' AND shulam=0 AND agent_person_number={$_SESSION['person_number']}  and DATE_ADD(create_2, INTERVAL 60 DAY)  <= '{$today}'   {$sidx} {$sord} ;";
   				else	
   				$filter_query .= "{$fieldName} LIKE '%".${(string)$fieldName}."%' AND ";
   			}
                          // echo $filter_query;
   			#----- view and filter sql query generator  ------#
   			
   			#----- get data from DB ------#
   			require("../includes/openConnection.php"); 
   			$q = mysql_query($filter_query) or die(mysql_error());
   			require("../includes/closeConnection.php");
   			#----- get data from DB ------#
   			
   			#----- return json ------#
   			$rows = array();
   			while($r = mysql_fetch_assoc($q)) 
   				{	
   				//	$r['perosnid'] = $r['id'];
   				//  unset($r['id']);
   				$rows[] = $r;
   				}
   			print json_encode($rows);
   			#----- return json ------#
   		}
   
   		
   	}
           
           public function  viewManuimPerAgentPerAmlaDate()
   	{
                       
   		if (  isset($_SESSION['person_number']) )
   		{
                       
                       
                       
                      $filter_query_limit="SELECT *, from_2 as from_2, until as until FROM manuy2client WHERE ";   
                      $filter_query="SELECT COUNT(*) AS NumberOfRecords FROM manuy2client WHERE  ";  
                      
                      $filter_query_body = "manuy_row LIKE '%".$this->manuimFields["manuy_row"]."%' AND ";
                      $filter_query_body .= "manuy_number LIKE '%".$this->manuimFields["manuy_number"]."%' AND ";
                      $filter_query_body .= "create_2 LIKE '%".$this->manuimFields["create_2"]."%' AND ";
                      $filter_query_body .= "state LIKE '%".$this->manuimFields["state"]."%' AND ";
                      $filter_query_body .= "agent_person_number LIKE '%".$_SESSION['person_number']."%' AND ";
                      $filter_query_body .= "person_number LIKE '%".$this->manuimFields["agent_person_number"]."%' AND ";
                      $filter_query_body .= "manufacturer LIKE '%".$this->manuimFields["manufacturer"]."%' AND ";
                      $filter_query_body .= "model LIKE '%".$this->manuimFields["model"]."%' AND ";
                      $filter_query_body .= "license_plate LIKE '%".$this->manuimFields["license_plate"]."%' AND ";
                      $filter_query_body .= "production_year LIKE '%".$this->manuimFields["production_year"]."%' AND ";
                      $filter_query_body .= "product_desc LIKE '%".$this->manuimFields["product_desc"]."%' AND ";
                      $filter_query_body .= "product_price LIKE '%".$this->manuimFields["product_price"]."%' AND ";
                      $filter_query_body .= "paymen_type LIKE '%".$this->manuimFields["paymen_type"]."%' AND ";
                      $filter_query_body .= "agent_commission LIKE '%".$this->manuimFields["agent_commission"]."%' AND ";
                      $filter_query_body .= "from_agent_commission LIKE '%".$this->manuimFields["from_agent_commission"]."%' AND ";
                      $filter_query_body .= "total_payment LIKE '%".$this->manuimFields["total_payment"]."%' AND ";
                      $filter_query_body .= "shulam LIKE '%".$this->manuimFields["shulam"]."%' AND ";
                      $filter_query_body .= "last LIKE '%".$this->manuimFields["last"]."%' AND ";
                      $filter_query_body .= "shulamToAgent LIKE '%".$this->manuimFields["shulamToAgent"]."%' AND ";
                      $filter_query_body .= "shulamToMetriaAgent LIKE '%".$this->manuimFields["shulamToMetriaAgent"]."%' AND ";
                   //   $filter_query_body .= "agentAmlaDate LIKE '%".$this->manuimFields["agentAmlaDate"]."%' AND ";
                      $filter_query_body .= "agentAmlaDate BETWEEN '".$this->manuimFields["from_2"]."' AND '".$this->manuimFields["until"]."' ";
                      $filter_query_body .= "ORDER BY " .$this->manuimFields["sidx"]." ".$this->manuimFields["sord"]  ;
                      
                      $filter_query .=$filter_query_body;
                          
                      $filter_query_limit .=$filter_query_body. " LIMIT " .(($this->manuimFields["page"]-1)*$this->manuimFields["rows"])." , " .(($this->manuimFields["page"])*$this->manuimFields["rows"]);
   
                      //echo $filter_query_limit;
   
   
                      #----- get data from DB ------#
                      require("../includes/openConnection.php"); 
                      $filter_query_result = mysql_query($filter_query) or die(mysql_error());
                      $filter_query_limit_result = mysql_query($filter_query_limit) or die(mysql_error());
                      require("../includes/closeConnection.php");
                      #----- get data from DB ------#
   		
                   
                      #----- return json ------#
                      $jsonData = array();
                      $rows = array();
                      while($r1 = mysql_fetch_assoc($filter_query_limit_result)) 
                               $rows[] = $r1;
   
   
                      $r2 = mysql_fetch_assoc($filter_query_result);
                      $jsonData['records'] = $r2['NumberOfRecords'];
                      $jsonData['total']= ceil($jsonData['records']/$this->manuimFields["rows"]);
                      $jsonData['page']=$this->manuimFields["page"];
                      $jsonData['rows']=$rows;
   
                      print json_encode($jsonData);
                       
                       
                    
   		}
   
   		
   	}
         
           public function  getIncomesAndAmlotPerAgent()
   	{
   
               if (  isset($_SESSION['person_number']) )
   		{
                      $filter_query_limit="SELECT SUM(agent_commission) AS sum_agent_commission,SUM(total_payment) AS sum_total_payment FROM manuy2client WHERE ";   
                      $filter_query_body = "manuy_row LIKE '%".$this->manuimFields["manuy_row"]."%' AND ";
                      $filter_query_body .= "manuy_number LIKE '%".$this->manuimFields["manuy_number"]."%' AND ";
                      $filter_query_body .= "create_2 LIKE '%".$this->manuimFields["create_2"]."%' AND ";
                      $filter_query_body .= "state LIKE '%".$this->manuimFields["state"]."%' AND ";
                      $filter_query_body .= "agent_person_number LIKE '%".$_SESSION['person_number']."%' AND ";
                      $filter_query_body .= "person_number LIKE '%".$this->manuimFields["agent_person_number"]."%' AND ";
                      $filter_query_body .= "manufacturer LIKE '%".$this->manuimFields["manufacturer"]."%' AND ";
                      $filter_query_body .= "model LIKE '%".$this->manuimFields["model"]."%' AND ";
                      $filter_query_body .= "license_plate LIKE '%".$this->manuimFields["license_plate"]."%' AND ";
                      $filter_query_body .= "production_year LIKE '%".$this->manuimFields["production_year"]."%' AND ";
                      $filter_query_body .= "product_desc LIKE '%".$this->manuimFields["product_desc"]."%' AND ";
                      $filter_query_body .= "product_price LIKE '%".$this->manuimFields["product_price"]."%' AND ";
                      $filter_query_body .= "paymen_type LIKE '%".$this->manuimFields["paymen_type"]."%' AND ";
                      $filter_query_body .= "agent_commission LIKE '%".$this->manuimFields["agent_commission"]."%' AND ";
                      $filter_query_body .= "from_agent_commission LIKE '%".$this->manuimFields["from_agent_commission"]."%' AND ";
                      $filter_query_body .= "total_payment LIKE '%".$this->manuimFields["total_payment"]."%' AND ";
                      $filter_query_body .= "shulam LIKE '%".$this->manuimFields["shulam"]."%' AND ";
                      $filter_query_body .= "last LIKE '%".$this->manuimFields["last"]."%' AND ";
                      $filter_query_body .= "shulamToAgent LIKE '%".$this->manuimFields["shulamToAgent"]."%' AND ";
                      $filter_query_body .= "shulamToMetriaAgent LIKE '%".$this->manuimFields["shulamToMetriaAgent"]."%' AND ";
                   //   $filter_query_body .= "agentAmlaDate LIKE '%".$this->manuimFields["agentAmlaDate"]."%' AND ";
                      $filter_query_body .= "agentAmlaDate BETWEEN '".$this->manuimFields["from_2"]."' AND '".$this->manuimFields["until"]."' ";
                      $filter_query_body .= "ORDER BY " .$this->manuimFields["sidx"]." ".$this->manuimFields["sord"]  ;
                      
                      
                          
                      $filter_query_limit .=$filter_query_body. " LIMIT " .(($this->manuimFields["page"]-1)*$this->manuimFields["rows"])." , " .(($this->manuimFields["page"])*$this->manuimFields["rows"]);
   
                      //echo $filter_query_limit;
                      #----- get data from DB ------#
                      require("../includes/openConnection.php"); 
                      $filter_query_limit_result = mysql_query($filter_query_limit) or die(mysql_error());
                      require("../includes/closeConnection.php");
                      #----- get data from DB ------#
   		
                   
                      #----- return json ------#
                      $jsonData = array();
                      while($r1 = mysql_fetch_assoc($filter_query_limit_result)) 
                               $jsonData[] = $r1;
   
                      
                      print json_encode($jsonData);
   
   		}
   	
   	}
           
           
            public function  viewManuimPerAgentPerAmlaDateOfMtriaAgent()
   	{
                       
   		if (  isset($_SESSION['person_number']) )
   		{
   			#----- view and filter sql query generator  ------#
   			$oneYearOn = date('Y-m-d',strtotime(date("Y-m-d", time()) . " + 365 day"));
                          $oneYearBack = date('Y-m-d',strtotime(date("Y-m-d", time()) . " - 365 day"));
   			#----- view and filter sql query generator  ------#
   			$filter_query="SELECT agentAmlaDate,agent_commission,agent_person_number,create_2, from_2 as from_2, until as until,from_agent_commission,last,license_plate,manufacturer,manuy_number,manuy_row,model,manuy2client.person_number,product_desc,product_price,production_year,shulam,shulamToAgent,shulamToMetriaAgent,state,total_payment FROM agent,manuy2client WHERE ";
   			$sidx = ( isset($_POST['sidx']) ? 'ORDER BY '.$_POST['sidx'] : "");	
   			$sord = ( isset($_POST['sord']) ? $_POST['sord'] : "");	
                           $from_2 = ( isset($_POST['from_2']) ? $_POST['from_2'] : $oneYearBack);	
   			$until = ( isset($_POST['until']) ? $_POST['until'] : $oneYearOn);
   			foreach ($this->viewManuimPerAgentNames as $fieldName) 
   			{
   				${(string)$fieldName} = ( isset($_POST[(string)$fieldName]) ? $_POST[(string)$fieldName] : "");	
   				if ($fieldName === end($this->viewManuimPerAgentNames))
   				$filter_query .= "{$fieldName} LIKE '%".${(string)$fieldName}."%' AND underagent ={$_SESSION['person_number']} AND agent.person_number=manuy2client.agent_person_number and agentAmlaDate BETWEEN '{$from_2}' and  '{$until}'  {$sidx} {$sord} ;";
   				else	
   				$filter_query .= "{$fieldName} LIKE '%".${(string)$fieldName}."%' AND ";
   			}
   			//echo $filter_query;
   			#----- view and filter sql query generator  ------#
   			
   			#----- get data from DB ------#
   			require("../includes/openConnection.php"); 
   			$q = mysql_query($filter_query) or die(mysql_error());
   			require("../includes/closeConnection.php");
   			#----- get data from DB ------#
   			
   			#----- return json ------#
   			$rows = array();
   			while($r = mysql_fetch_assoc($q)) 
   				{	
   				//	$r['perosnid'] = $r['id'];
   				//  unset($r['id']);
   				$rows[] = $r;
   				}
   			print json_encode($rows);
   			#----- return json ------#
   		}
   
   		
   	}
           
           
          
           public function  getIncomesAndAmlotPerMetriaAgent()
   	{
                       
   		if (  isset($_SESSION['person_number']) )
   		{
   			#----- view and filter sql query generator  ------#
   			$oneYearOn = date('Y-m-d',strtotime(date("Y-m-d", time()) . " + 365 day"));
                           $oneYearBack = date('Y-m-d',strtotime(date("Y-m-d", time()) . " - 365 day"));
   			#----- view and filter sql query generator  ------#
   			$filter_query="SELECT COUNT(DISTINCT(manuy_number)) as manuimNumber FROM agent,manuy2client WHERE ";
   			$sidx = ( isset($_POST['sidx']) ? 'ORDER BY '.$_POST['sidx'] : "");	
   			$sord = ( isset($_POST['sord']) ? $_POST['sord'] : "");	
                           $from_2 = ( isset($_POST['from_2']) ? $_POST['from_2'] : $oneYearBack);	
   			$until = ( isset($_POST['until']) ? $_POST['until'] : $oneYearOn);
   			foreach ($this->viewManuimPerAgentNames as $fieldName) 
   			{
   				${(string)$fieldName} = ( isset($_POST[(string)$fieldName]) ? $_POST[(string)$fieldName] : "");	
   				if ($fieldName === end($this->viewManuimPerAgentNames))
   				$filter_query .= "{$fieldName} LIKE '%".${(string)$fieldName}."%' AND underagent ={$_SESSION['person_number']} AND agent.person_number=manuy2client.agent_person_number and agentAmlaDate BETWEEN '{$from_2}' and  '{$until}'  {$sidx} {$sord} ;";
   				else	
   				$filter_query .= "{$fieldName} LIKE '%".${(string)$fieldName}."%' AND ";
   			}
                           
                           $filter_query2="SELECT metriaAmla  FROM  agent WHERE person_number={$_SESSION['person_number']} ";
   			//echo $filter_query;
   			#----- view and filter sql query generator  ------#
   			
   			#----- get data from DB ------#
   			require("../includes/openConnection.php"); 
   			$q = mysql_query($filter_query) or die(mysql_error());
                           $q2 = mysql_query($filter_query2) or die(mysql_error());
   			require("../includes/closeConnection.php");
   			#----- get data from DB ------#
   			
   			#----- return json ------#
   			$rows = array();
   			while($r = mysql_fetch_assoc($q)) 
   				{	
   				//	$r['perosnid'] = $r['id'];
   				//  unset($r['id']);
   				$rows[] = $r;
   				}
                                   
                           while($r = mysql_fetch_assoc($q2)) 
                           {	
                           //	$r['perosnid'] = $r['id'];
                           //  unset($r['id']);
                           $rows[] = $r;
                           }
   			print json_encode($rows);
   			#----- return json ------#
   		}
   
   		
   	}
             
            
           /*
           public function  updateAmlotDateInMauny()
   	{
               
               if (  isset($_SESSION['person_number']) )
   		{
   	
   			$filter_query="SELECT recived as allDates
                                       FROM  cash
                                       WHERE manuy2client_manuy_number=65
                                       union
                                       SELECT repayment_day 
                                       FROM  checkes
                                       WHERE manuy2client_manuy_number={$_POST['manuy2client_manuy_number']}";
   			
   			require("../includes/openConnection.php"); 
   			$q = mysql_query($filter_query) or die(mysql_error());
   			require("../includes/closeConnection.php");
   			#----- get data from DB ------#
   			
   			#----- return json ------#
   			$rows = array();
   			while($r = mysql_fetch_assoc($q)) 
   				{	
   				//	$r['perosnid'] = $r['id'];
   				//  unset($r['id']);
   				$rows[] = $r;
   				}
   			print json_encode($rows);
   			#----- return json ------#
   		}
   
           
               
           }
           
           
           public function  getIncomesAndAmlotPerAgent()
   	{    
   		if (  isset($_SESSION['person_number']) )
   		{
   			#----- view and filter sql query generator  ------#
   			$oneYearOn = date('Y-m-d',strtotime(date("Y-m-d", time()) . " + 365 day"));
                           $oneYearBack = date('Y-m-d',strtotime(date("Y-m-d", time()) . " - 365 day"));
   			#----- view and filter sql query generator  ------#
   			$filter_query="SELECT SUM(agent_commission) AS sum_agent_commission,SUM(total_payment) AS sum_total_payment FROM manuy2client WHERE";
   			$sidx = ( isset($_POST['sidx']) ? 'ORDER BY '.$_POST['sidx'] : "");	
   			$sord = ( isset($_POST['sord']) ? $_POST['sord'] : "");	
                           $from_2 = ( isset($_POST['from_2']) ? $_POST['from_2'] : $oneYearBack);	
   			$until = ( isset($_POST['until']) ? $_POST['until'] : $oneYearOn);
   			foreach ($this->viewManuimPerAgentNamesAmlot as $fieldName) 
   			{
   				${(string)$fieldName} = ( isset($_POST[(string)$fieldName]) ? $_POST[(string)$fieldName] : "");	
   				if ($fieldName === end($this->viewManuimPerAgentNamesAmlot))
   				$filter_query .= "{$fieldName} LIKE '%".${(string)$fieldName}."%'  AND agent_person_number={$_SESSION['person_number']} and agentAmlaDate BETWEEN '{$from_2}' and  '{$until}'  {$sidx} {$sord} ;";
   				else	
   				$filter_query .= "{$fieldName} LIKE '%".${(string)$fieldName}."%' AND ";
   			}
   			//echo $filter_query;
   			#----- view and filter sql query generator  ------#
   			
   			#----- get data from DB ------#
   			require("../includes/openConnection.php"); 
   			$q = mysql_query($filter_query) or die(mysql_error());
   			require("../includes/closeConnection.php");
   			#----- get data from DB ------#
   			
   			#----- return json ------#
   			$rows = array();
   			while($r = mysql_fetch_assoc($q)) 
   				{	
   				//	$r['perosnid'] = $r['id'];
   				//  unset($r['id']);
   				$rows[] = $r;
   				}
   			print json_encode($rows);
   			#----- return json ------#
   		}
   
   		
   	}
   	*/
           	public function  viewManuims()
   	{
                      $filter_query_limit="SELECT *, from_2 as from_2, until as until FROM manuy2client WHERE ";   
                      $filter_query="SELECT COUNT(*) AS NumberOfRecords FROM manuy2client WHERE  ";  
                      
                      $filter_query_body = "manuy_row LIKE '%".$this->manuimFields["manuy_row"]."%' AND ";
                      $filter_query_body .= "manuy_number LIKE '%".$this->manuimFields["manuy_number"]."%' AND ";
                      $filter_query_body .= "create_2 LIKE '%".$this->manuimFields["create_2"]."%' AND ";
                      $filter_query_body .= "state LIKE '%".$this->manuimFields["state"]."%' AND ";
                      $filter_query_body .= "agent_person_number LIKE '%".$this->manuimFields["agent_person_number"]."%' AND ";
                      $filter_query_body .= "person_number LIKE '%".$this->manuimFields["person_number"]."%' AND ";
                      $filter_query_body .= "manufacturer LIKE '%".$this->manuimFields["manufacturer"]."%' AND ";
                      $filter_query_body .= "model LIKE '%".$this->manuimFields["model"]."%' AND ";
                      $filter_query_body .= "license_plate LIKE '%".$this->manuimFields["license_plate"]."%' AND ";
                      $filter_query_body .= "production_year LIKE '%".$this->manuimFields["production_year"]."%' AND ";
                      $filter_query_body .= "product_desc LIKE '%".$this->manuimFields["product_desc"]."%' AND ";
                      $filter_query_body .= "product_price LIKE '%".$this->manuimFields["product_price"]."%' AND ";
                      $filter_query_body .= "paymen_type LIKE '%".$this->manuimFields["paymen_type"]."%' AND ";
                      $filter_query_body .= "agent_commission LIKE '%".$this->manuimFields["agent_commission"]."%' AND ";
                      $filter_query_body .= "from_agent_commission LIKE '%".$this->manuimFields["from_agent_commission"]."%' AND ";
                      $filter_query_body .= "total_payment LIKE '%".$this->manuimFields["total_payment"]."%' AND ";
                      $filter_query_body .= "shulam LIKE '%".$this->manuimFields["shulam"]."%' AND ";
                      $filter_query_body .= "last LIKE '%".$this->manuimFields["last"]."%' AND ";
                      $filter_query_body .= "shulamToAgent LIKE '%".$this->manuimFields["shulamToAgent"]."%' AND ";
                      $filter_query_body .= "shulamToMetriaAgent LIKE '%".$this->manuimFields["shulamToMetriaAgent"]."%' AND ";
                      $filter_query_body .= "agentAmlaDate LIKE '%".$this->manuimFields["agentAmlaDate"]."%' AND ";
                      
                      $filter_query_body .= "from_2 BETWEEN '".$this->manuimFields["from_2"]."' AND '".$this->manuimFields["until"]."' ";
                      $filter_query_body .= "ORDER BY " .$this->manuimFields["sidx"]." ".$this->manuimFields["sord"]  ;
                      
                      $filter_query .=$filter_query_body;
                          
                      $filter_query_limit .=$filter_query_body. " LIMIT " .(($this->manuimFields["page"]-1)*$this->manuimFields["rows"])." , " .(($this->manuimFields["page"])*$this->manuimFields["rows"]);
   
                      //echo $filter_query_limit;
   
   
                      #----- get data from DB ------#
                      require("../includes/openConnection.php"); 
                      $filter_query_result = mysql_query($filter_query) or die(mysql_error());
                      $filter_query_limit_result = mysql_query($filter_query_limit) or die(mysql_error());
                      require("../includes/closeConnection.php");
                      #----- get data from DB ------#
   		
                   
                      #----- return json ------#
                      $jsonData = array();
                      $rows = array();
                      while($r1 = mysql_fetch_assoc($filter_query_limit_result)) 
                               $rows[] = $r1;
   
   
                      $r2 = mysql_fetch_assoc($filter_query_result);
                      $jsonData['records'] = $r2['NumberOfRecords'];
                      $jsonData['total']= ceil($jsonData['records']/$this->manuimFields["rows"]);
                      $jsonData['page']=$this->manuimFields["page"];
                      $jsonData['rows']=$rows;
   
                      print json_encode($jsonData);
   		
   	}
   	public function  viewManuimsSum()
   	{
            $filter_query_limit="SELECT SUM(agent_commission) AS sum_agent_commission,SUM(total_payment) AS sum_total_payment,SUM(product_price) AS sum_product_price FROM manuy2client WHERE ";
                   
                      $filter_query_body = "manuy_row LIKE '%".$this->manuimFields["manuy_row"]."%' AND ";
                      $filter_query_body .= "manuy_number LIKE '%".$this->manuimFields["manuy_number"]."%' AND ";
                      $filter_query_body .= "create_2 LIKE '%".$this->manuimFields["create_2"]."%' AND ";
                      $filter_query_body .= "state LIKE '%".$this->manuimFields["state"]."%' AND ";
                      $filter_query_body .= "agent_person_number LIKE '%".$this->manuimFields["agent_person_number"]."%' AND ";
                      $filter_query_body .= "person_number LIKE '%".$this->manuimFields["person_number"]."%' AND ";
                      $filter_query_body .= "manufacturer LIKE '%".$this->manuimFields["manufacturer"]."%' AND ";
                      $filter_query_body .= "model LIKE '%".$this->manuimFields["model"]."%' AND ";
                      $filter_query_body .= "license_plate LIKE '%".$this->manuimFields["license_plate"]."%' AND ";
                      $filter_query_body .= "production_year LIKE '%".$this->manuimFields["production_year"]."%' AND ";
                      $filter_query_body .= "product_desc LIKE '%".$this->manuimFields["product_desc"]."%' AND ";
                      $filter_query_body .= "product_price LIKE '%".$this->manuimFields["product_price"]."%' AND ";
                      $filter_query_body .= "paymen_type LIKE '%".$this->manuimFields["paymen_type"]."%' AND ";
                      $filter_query_body .= "agent_commission LIKE '%".$this->manuimFields["agent_commission"]."%' AND ";
                      $filter_query_body .= "from_agent_commission LIKE '%".$this->manuimFields["from_agent_commission"]."%' AND ";
                      $filter_query_body .= "total_payment LIKE '%".$this->manuimFields["total_payment"]."%' AND ";
                      $filter_query_body .= "shulam LIKE '%".$this->manuimFields["shulam"]."%' AND ";
                      $filter_query_body .= "last LIKE '%".$this->manuimFields["last"]."%' AND ";
                      $filter_query_body .= "shulamToAgent LIKE '%".$this->manuimFields["shulamToAgent"]."%' AND ";
                      $filter_query_body .= "shulamToMetriaAgent LIKE '%".$this->manuimFields["shulamToMetriaAgent"]."%' AND ";
                      $filter_query_body .= "agentAmlaDate LIKE '%".$this->manuimFields["agentAmlaDate"]."%' AND ";
                      
                      $filter_query_body .= "from_2 BETWEEN '".$this->manuimFields["from_2"]."' AND '".$this->manuimFields["until"]."' ";
                      $filter_query_body .= "ORDER BY " .$this->manuimFields["sidx"]." ".$this->manuimFields["sord"]  ;
                      
                     
                          
                      $filter_query_limit .=$filter_query_body. " LIMIT " .(($this->manuimFields["page"]-1)*$this->manuimFields["rows"])." , " .(($this->manuimFields["page"])*$this->manuimFields["rows"]);
   
                         #----- get data from DB ------#
                      require("../includes/openConnection.php"); 
                      
                      $filter_query_limit_result = mysql_query($filter_query_limit) or die(mysql_error());
                      require("../includes/closeConnection.php");
                      #----- get data from DB ------#
   
   
                   
                      #----- return json ------#
                      $jsonData = array();
                      $rows = array();
                      while($r1 = mysql_fetch_assoc($filter_query_limit_result)) 
                               $rows[] = $r1;
   
                      print json_encode($rows);
   		
   	}
   	
   	public function  editManuim()
   	{
               $sql_query="UPDATE  manuy2client SET  `manuy_number` ='".$this->manuimFields["manuy_number"]."',
               `create_2` ='".$this->manuimFields["create_2"]."',
               `state` ='".$this->manuimFields["state"]."',
               `agent_person_number` ='".$this->manuimFields["agent_person_number"]."',
               `person_number` ='".$this->manuimFields["person_number"]."',
               `from_2` ='".$this->manuimFields["from_2"]."',
               `until` ='".$this->manuimFields["until"]."',
               `manufacturer` ='".$this->manuimFields["manufacturer"]."',
               `model` ='".$this->manuimFields["model"]."',
               `license_plate` ='".$this->manuimFields["license_plate"]."',
               `production_year` ='".$this->manuimFields["production_year"]."',
               `product_desc` ='".$this->manuimFields["product_desc"]."',
               `product_price` ='".$this->manuimFields["product_price"]."',
               `paymen_type` ='".$this->manuimFields["paymen_type"]."',
               `agent_commission` ='".$this->manuimFields["agent_commission"]."',
               `from_agent_commission` ='".$this->manuimFields["from_agent_commission"]."',
               `total_payment` ='".$this->manuimFields["total_payment"]."',
               `shulam` ='".$this->manuimFields["shulam"]."',
               `last` ='".$this->manuimFields["last"]."',
               `shulamToAgent` ='".$this->manuimFields["shulamToAgent"]."',
               `shulamToMetriaAgent` ='".$this->manuimFields["shulamToMetriaAgent"]."',
               `agentAmlaDate` ='".$this->manuimFields["agentAmlaDate"]."' WHERE  manuy_row ='".$this->manuimFields["manuy_row"]."';";
               
             // echo $sql_query;
                   #----- insert data to DB ------#
   		require("../includes/openConnection.php"); 
                   if(
   		mysql_query($sql_query)  
                   ) echo "ok"; else  echo mysql_error();
   		$manuy_row=mysql_insert_id();
   		require("../includes/closeConnection.php");
   		#----- insert data to DB ------#	
   		return $manuy_row;
               	
   	}
   	
   	public function  get_Mehiron_Names_With_Prices()
   	{
   		#----- insert data to DB ------#
   		require("../includes/openConnection.php");  
   		$q1=mysql_query("SELECT * FROM  mehiron_names;");
   		$q2=mysql_query("SELECT  product1, product2, product3, product4, product5, product6, product7, product8, product9, product10, product11, product12, product13, product14, product15, product16 FROM  mehiron WHERE mehiron_number='{$_POST ['mehiron_number']}' ;");
   		require("../includes/closeConnection.php");
   		#----- insert data to DB ------#	
   		
   		#----- return json ------#
                    $data = array();
   		$namesArray = array();
                   $pricesArray = array();
   		$r1 = mysql_fetch_array($q1);
   		$r2 = mysql_fetch_array($q2);
   		for($i=0; $i<=15; $i++)
   			{	
                       $namesArray[$i]=$r1[$i];
                       $pricesArray[$i]=$r2[$i];
   			//$rows[$r2[$i] ] = $r1[$i];
                           //$rows[$r1[$i] ] = $r2[$i];
   			
   			}
   			$data['names'] = $namesArray;
                           $data['prices'] = $pricesArray;
                           print json_encode($data);
   	}
   		#----- return json ------#
                   
                   
                  
   
   	
   	public function  addManuim()
   	{
   		
               foreach ($this->manuimFieldNames as $fieldName) 
               ${(string)$fieldName} = ( isset($_POST[(string)$fieldName]) ? $_POST[(string)$fieldName] : "");
   
               $today=date("Y-m-d");
               #----- insert data to DB ------#
               require("../includes/openConnection.php");
               if($manuy_number=="" || $manuy_number==null || $state=="חידוש") #----- if this is new manuy ------# מספר המנוי הוא המונה פלוס אחד
               {
                   if(mysql_query("UPDATE varibles SET manuyNumberCounter = manuyNumberCounter + 1 ;")) { 
                   $q1=mysql_query("SELECT manuyNumberCounter FROM varibles;");
                   $r1 = mysql_fetch_assoc($q1);
                   $manuy_number=$r1["manuyNumberCounter"]; 
                   $state="פעיל";
                   }
               }
               else
               {
                   
                  // if($state!="קפוא")
                   mysql_query("UPDATE manuy2client SET `last` = '0' WHERE manuy_row ='{$manuy_row}';");
                   
                   if($state=="הפשרה והפעלה")
                   {  
                       $state="החזר עקב הקפאה";
                       mysql_query("UPDATE manuy2client SET `total_payment` = '{$total_payment}',`product_price` = '{product_price}',`state` = '{$state}',`from_2` = '{$from_2}',`until` = '{$until}',`agent_commission` = '{$agent_commission}' WHERE manuy_row ='{$manuy_row}';") or die ("Error in query: $query. ".mysql_error());
                    $state="פעיל";
                       $untilBeforeChange= $_POST['untilBeforeChange']  ; 
                       $until=$untilBeforeChange;
                       $agent_commission=0;
                               $product_price=0;
                               $total_payment=0;
                   }
                   

                   
                   if( $state=="החלפה")
                   {
                   $amlaPriceBeforeChange=$_POST['amlaPriceBeforeChange']*(-1) ; 
                   $totalPriceBeforeChange= $_POST['totalPriceBeforeChange']*(-1)  ; 
                   $manuyPriceBeforeChange= $_POST['manuyPriceBeforeChange']*(-1)  ; 
                   $manuyDescBeforeChange= $_POST['manuyDescBeforeChange'] ; 
                   
                   $state="בוטל עקב החלפה";
                   $manuy_row=NULL;
                   if(mysql_query("INSERT INTO manuy2client (`manuy_row`, `manuy_number`, `create_2`, `state`, `agent_person_number`, `person_number`, `from_2`, `until`, `manufacturer`, `model`, `license_plate`, `production_year`, `product_desc`, `product_price`, `paymen_type`, `agent_commission`, `from_agent_commission`, `total_payment`, `shulam`, `last`, `shulamToAgent`, `shulamToMetriaAgent`,`agentAmlaDate`) VALUES('{$manuy_row}', '{$manuy_number}', '{$today}', '{$state}', '{$agent_person_number}', '{$person_number}', '{$from_2}', '{$until}', '{$manufacturer}', '{$model}', '{$license_plate}', '{$production_year}', '{$manuyDescBeforeChange}', '{$manuyPriceBeforeChange}', '{$paymen_type}', '{$amlaPriceBeforeChange}', '{$from_agent_commission}', '{$totalPriceBeforeChange}', '{$shulam}', '0', '{$shulamToAgent}', '{$shulamToMetriaAgent}', '{$from_2}');"))
                   echo "";
                   $state="פעיל";
                   }

                   
                   

               }
               //echo $manuy_row;
               
               $manuy_row=NULL;
              // echo "INSERT INTO manuy2client (`manuy_row`, `manuy_number`, `create_2`, `state`, `agent_person_number`, `person_number`, `from_2`, `until`, `manufacturer`, `model`, `license_plate`, `production_year`, `product_desc`, `product_price`, `paymen_type`, `agent_commission`, `from_agent_commission`, `total_payment`, `shulam`, `last`, `shulamToAgent`, `shulamToMetriaAgent`,`agentAmlaDate`) VALUES('{$manuy_row}', '{$manuy_number}', '{$today}', '{$state}', '{$agent_person_number}', '{$person_number}', '{$from_2}', '{$until}', '{$manufacturer}', '{$model}', '{$license_plate}', '{$production_year}', '{$product_desc}', '{$product_price}', '{$paymen_type}', '{$agent_commission}', '{$from_agent_commission}', '{$total_payment}', '{$shulam}', '1', '{$shulamToAgent}', '{$shulamToMetriaAgent}', '{$from_2}');";
               if(mysql_query("INSERT INTO manuy2client (`manuy_row`, `manuy_number`, `create_2`, `state`, `agent_person_number`, `person_number`, `from_2`, `until`, `manufacturer`, `model`, `license_plate`, `production_year`, `product_desc`, `product_price`, `paymen_type`, `agent_commission`, `from_agent_commission`, `total_payment`, `shulam`, `last`, `shulamToAgent`, `shulamToMetriaAgent`,`agentAmlaDate`) VALUES('{$manuy_row}', '{$manuy_number}', '{$today}', '{$state}', '{$agent_person_number}', '{$person_number}', '{$from_2}', '{$until}', '{$manufacturer}', '{$model}', '{$license_plate}', '{$production_year}', '{$product_desc}', '{$product_price}', '{$paymen_type}', '{$agent_commission}', '{$from_agent_commission}', '{$total_payment}', '{$shulam}', '1', '{$shulamToAgent}', '{$shulamToMetriaAgent}', '{$from_2}');"))
               echo "ok";
   
               
               $manuy_row=mysql_insert_id(); 
               require("../includes/closeConnection.php");
               #----- insert data to DB ------#	
               return $manuy_number;
   
   	}
   	
   	public function  deleteManuim()
   	{
   		#----- delete data to DB ------#
   		require("../includes/openConnection.php");  
   		if(mysql_query("DELETE FROM manuy2client WHERE manuy_row='{$_POST ['manuy_row']}';")) echo "ok";
   		require("../includes/closeConnection.php");
   		#----- delete data to DB ------#	
   			
   	}
   	public function  approveManuy()
   	{
   		#----- delete data to DB ------#
   		require("../includes/openConnection.php");  
   		 mysql_query("UPDATE manuy2client SET notification_approved = 1 WHERE manuy_row ='{$this->manuimFields["manuy_row"]}';");
   		require("../includes/closeConnection.php");
   		#----- delete data to DB ------#	
   			
   	}
           
           public function  getFirstCreateDate()
   	{
   		
   		require("../includes/openConnection.php");  
   		if($q =mysql_query("SELECT min(create_2) as firstDate FROM  `manuy2client` WHERE manuy_number='{$_POST ['manuy_number']}';")) 
                   {
                       $r = mysql_fetch_assoc($q);
                       echo $r["firstDate"];
                   }
                   echo false;	
   			
   	}
   
   }
   
   
   
   
   ?>